/*
This file is part of IRCaBot.
IRCaBot is IRC logger with features.
Source code: https://notabug.org/acetone/ircabot.
Copyright (C) acetone, 2023.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

#pragma once

#include <QString>
#include <QMap>
#include <QJsonObject>
#include <QFile>

namespace http {

constexpr const char CODE_200[] = "200 OK";
constexpr const char CODE_303[] = "303 See Other";
constexpr const char CODE_304[] = "304 Not Modified";
constexpr const char CODE_400[] = "400 Bad request";
constexpr const char CODE_403[] = "403 Access Denied";
constexpr const char CODE_404[] = "404 Not Found";
constexpr const char CODE_406[] = "406 Not Acceptable";
constexpr const char CODE_413[] = "413 Payload Too Large";
constexpr const char CODE_500[] = "500 Internal Error";

constexpr const char CONTENT_TYPE_HTML[]  = "text/html; charset=utf-8";
constexpr const char CONTENT_TYPE_TEXT[]  = "text/plain; charset=utf-8";
constexpr const char CONTENT_TYPE_CSS[]   = "text/css";
constexpr const char CONTENT_TYPE_SVG[]   = "image/svg+xml";
constexpr const char CONTENT_TYPE_ICO[]   = "image/ico";
constexpr const char CONTENT_TYPE_PNG[]   = "image/png";
constexpr const char CONTENT_TYPE_GIF[]   = "image/gif";
constexpr const char CONTENT_TYPE_JS[]    = "text/javascript; charset=utf-8";
constexpr const char CONTENT_TYPE_WOFF[]  = "font/woff";
constexpr const char CONTENT_TYPE_WOFF2[] = "font/woff2";
constexpr const char CONTENT_TYPE_JSON[]  = "application/json; charset=utf-8";
constexpr const char CONTENT_TYPE_MP3[]   = "audio/mpeg";
constexpr const char CONTENT_TYPE_BIN[]   = "application/octet-stream";

constexpr const char HEADER_ContentType[]   = "Content-Type";
constexpr const char HEADER_ContentLength[] = "Content-Length";
constexpr const char HEADER_ETag[]          = "ETag";
constexpr const char HEADER_SetCookie[]     = "Set-Cookie";

} // namespace http

class HttpDocument
{
public:
    HttpDocument() = default;
    void setCode(const QString& code = http::CODE_200);
    QString code() const { return m_code; }

    void setHeader(const QString& name, const QString& value);
    const QMap<QString, QString>& headers() const { return m_headers; }

    void setBody(const QByteArray& body);
    void setBody(const QJsonObject& body);
    void setBody(QFile&& payload, const QString& eTag = QString());
    void setBody(const QString& body, const QString& eTag);
    void setBodyStandardError(const QString& text, const QString& title = "Error", bool systemError = false, const QString& backLink = "");
    QByteArray body() const { return m_body; }

    QByteArray document() const;

private:
    QString m_code = http::CODE_200;
    QMap<QString, QString> m_headers;
    QByteArray m_body;
};
